package com.syz.mall.controller;

import com.syz.mall.pojo.User;

import com.syz.mall.pojo.query.UserQuery;
import com.syz.mall.service.ILoginLogService;
import com.syz.mall.service.IUserService;

import com.syz.mall.util.IPUtil;
import com.syz.mall.util.LayUITableResult;
import com.syz.mall.util.MD5Util;
import com.syz.mall.util.Result;
import com.syz.mall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private ILoginLogService loginLogService;

    @RequestMapping("/login")
    @ResponseBody
    public Result login(HttpServletRequest request,String code, String username, String password, HttpSession session) {
        String codeInSession = (String) session.getAttribute("codeInSession");

        if (!codeInSession.equalsIgnoreCase(code)) {
            return Result.error("验证码错误");
        }
        String md5password= MD5Util.MD5Encode(password);
        User user = userService.login(username, md5password);
        if (user != null) {
            if(user.getStatus().equals(0)||user.getStatus()==0){
                loginLogService.recordLoginLog(user.getUsername(), 0, IPUtil.getIPAddress(request), "登录失败");
                return Result.error("此账户已被禁用");
            }
            session.setAttribute("user", user);
            session.setMaxInactiveInterval(0); //设置会话时长
            loginLogService.recordLoginLog(user.getUsername(), 1, IPUtil.getIPAddress(request), "登录成功");
            return Result.ok("登录成功");
        } else {
            loginLogService.recordLoginLog(user.getUsername(), 0, IPUtil.getIPAddress(request), "登录失败");
            return Result.error("账号或密码错误");
        }
    }

    @RequestMapping("/getLoginPage")
    public String getLoginPage() {
        return "UserManage/login";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "UserManage/login";
    }

    @RequestMapping("/getUserListPage")
    public String getUserListPage(){
        return "/UserManage/user_list";
    }
    @RequestMapping("/selectAll")
    @ResponseBody
    public LayUITableResult selectALl(UserQuery userQuery){
        return userService.selectByPage(userQuery);
    }

    @RequestMapping("updateSelective")
    @ResponseBody
    public Result updateSelective(User user){
        if(userService.updateSelective(user)){
            return Result.ok("更新成功");
        }else{
            return Result.error("更新失败");
        }
    }
}
